package com.hong.day05;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import java.util.List;

/**
 * @Auther: csh
 * @Date: 2019/11/25 17:52
 * @Description:组任务的测试
 */
public class GroupTest {

    //8任务交接，前提要保证当前用户是这个任务的负责人，这时候他才可以有权限去将任务交接给
    public static void main8(String[] args) {
        //1.得到ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.得到taskService
        TaskService taskService = processEngine.getTaskService();
        //3.设置一些参数，流程定义的key，候选用户
        String key = "myProcess_1";
        String assignee = "zhangsan";

        //4.执行查询
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(key)
                .singleResult();
        //5.判断是否有这个任务
        if(task!=null){
            //交接任务
            taskService.setAssignee(task.getId(),"lisi");
            System.out.println("交接任务完成~");
        }
    }


    //7.当前用户完成自己的任务
    public static void main(String[] args) {
        //1.得到ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.得到taskService
        TaskService taskService = processEngine.getTaskService();
        //3.设置一些参数，流程定义的key，候选用户
        String key = "holiday5";

        //4.执行查询
        Task zhangsan = taskService.createTaskQuery()
                .processDefinitionKey(key)
                .taskAssignee("lishi")
                .singleResult();
        if(zhangsan!=null){
            taskService.complete(zhangsan.getId());
        }
    }


    //6.当前的用户查询自己的任务
    public static void main6(String[] args) {
        //1.得到ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.得到taskService
        TaskService taskService = processEngine.getTaskService();
        //3.设置一些参数，流程定义的key，候选用户
        String key = "myProcess_1";

        //4.执行查询
        List <Task> lishi = taskService.createTaskQuery()
                .processDefinitionKey(key)
                .taskAssignee("zhangsan")
                .list();
        if(null!=lishi){
            for (Task task : lishi) {
                System.out.println(task.getProcessInstanceId());
                System.out.println(task.getId());
                System.out.println(task.getName());
                System.out.println(task.getAssignee());
            }
        }
    }

    //5.拾取用户
    public static void main5(String[] args) {
        //1.得到ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.得到taskService
        TaskService taskService = processEngine.getTaskService();
        //3.设置一些参数，流程定义的key，候选用户
        String key = "myProcess_1";
        String candidate_users="zhangsan";

        //4.执行查询
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(key)
                .taskCandidateUser(candidate_users)
                .singleResult();
        if(null!=task){
            taskService.claim(task.getId(),candidate_users);
            System.out.println("任务拾取完毕");
        }

    }

    //4.查询候选用户
    public static void main4(String[] args) {
        //1.得到ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.得到taskService
        TaskService taskService = processEngine.getTaskService();
        //3.设置一些参数，流程定义的key，候选用户
        String key = "myProcess_1";
        String candidate_users="zhangsan";

        //4.执行查询
        List <Task> list = taskService.createTaskQuery()
                .processDefinitionKey(key)
                .taskCandidateUser(candidate_users)
                .list();
        for (Task task : list) {
            System.out.println(task.getProcessInstanceId());
            System.out.println(task.getId());
            System.out.println(task.getName());
            System.out.println(task.getAssignee());
        }
    }

    //填写请假单的任务要执行完成
    public static void main3(String[] args) {
        //1.得到ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.得到taskService
        TaskService taskService = processEngine.getTaskService();
        //3.查询当前用户的任务
        Task task = taskService.createTaskQuery()
                .processDefinitionKey("myProcess_1")
                .taskAssignee("xiaozhang")
                .singleResult();

        //4.处理任务，结合当前用户列表的查询操作的话，任务ID5002
        if(task!=null){
            taskService.complete(task.getId());
            System.out.println("用户任务执行完毕！");
        }


        //5.输出任务的id
        System.out.println(task.getId());
    }


    /** 1.部署流程定义*/
    public static void main2(String[] args) {
        //1.创建defaultProcessEngine
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.得到RepositoryService
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        //3.进行部署
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("diagram/holiday5.bpmn")
                .name("请假申请单流程5")
                .deploy();
        System.out.println(deployment.getName());
        System.out.println(deployment.getId());
    }
    //自动流程实例
    public static void main1(String[] args) {
        //1.得到ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.得到RunService对象
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //3.创建流程实例 流程定义的key需要知道
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess_1");
        //4.输出实例的相关信息
        System.out.println("流程部署ID"+processInstance.getDeploymentId());
        System.out.println("流程实例ID"+processInstance.getId());
        System.out.println("活动ID"+processInstance.getActivityId());
    }
}
